package org.qydjk.console.controller.coupon;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.Coupon;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/coupon")
public class CouponController  extends BaseController {

    @RequestMapping("/couponpage.do")
    public String couponpage() {
        return "coupon/couponManage";
    }

    /**
     * 优惠券数据表格ajax
     * @return
     */
    @RequestMapping("/coupondg.do")
    @ResponseBody
    public Object coupondg(Integer page, Integer rows,Integer type,String begin,String end)  {
        try {
            HibernatePage pager = new HibernatePage(page, rows);
            String sql = "select * from qy_coupon where 1=1";
            String countsql = "select count(id) from qy_coupon where 1=1";
            if(type!=null){
                sql+=" and type="+type;
                countsql+=" and type="+type;
            }
            if(!StringUtil.isEmptyOrNull(begin)){
                sql+=" and start_time>'"+begin+"'";
                countsql+=" and start_time>'"+begin+"'";
            }
            if(!StringUtil.isEmptyOrNull(end)){
                sql+=" and end_time<'"+end+"'";
                countsql+=" and end_time<'"+end+"'";
            }
            List<Coupon> couponList = baseService.findBySQL(sql, Coupon.class, pager);
            int total = baseService.getRecordCountSQL(countsql);
            return new EasyUIResponse(total, couponList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加优惠券
     * @return
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Object add(Coupon coupon) throws Exception {
        try {
            coupon.setRemainRepertory(coupon.getRepertory()); //发放数量=剩余数量
            coupon.setCreateTime(new Date());//创建时间
            coupon.setUpdateTime(new Date()); //修改时间
            coupon.setStatus(1);
            baseService.saveOrUpdate(coupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /**
     * 修改优惠券
     * @return
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object edit(Coupon coupon) throws Exception {
        try {
            coupon.setUpdateTime(new Date()); //修改时间
            Coupon oldCoupon = baseService.get(Coupon.class,coupon.getId()); //获取原表
            int oldrepertory = oldCoupon.getRepertory();
            int test = oldrepertory-coupon.getRepertory();
            if(test>0){//减少库存
                if(test<=oldCoupon.getRemainRepertory()){
                    coupon.setRemainRepertory(oldCoupon.getRemainRepertory()-test);
                }else{
                    return renderError("减少的库存数不能大于剩余库存数");
                }
            }else{//增加库存
                int remainRe = oldCoupon.getRemainRepertory()-test;
                coupon.setRemainRepertory(remainRe);
            }
            BeanUtil.copyProperties(coupon,oldCoupon);
            baseService.saveOrUpdate(oldCoupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /**
     * 删除优惠券
     * @return
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object delete(Integer id) {
        try {
            Coupon coupon = baseService.get(Coupon.class,id);
            baseService.delete(coupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

    /**
     * 适用平台分类表格ajax
     * @return
     */
    @RequestMapping("/category.do")
    @ResponseBody
    public Object category(Integer id,Integer type)  {
        List<Map<String,Object>> categoryList = new ArrayList<Map<String, Object>>();
        try {
            if(id==null){return categoryList;}
            Coupon coupon = baseService.get(Coupon.class,id);
            String categoryStr = coupon.getSuitPlatformCategory();
            if(StringUtil.isEmptyOrNull(categoryStr)||",".equals(categoryStr)){
                if(type==2){
                    categoryList = baseService.findMapBySQL("select id,name,'add' as action from qy_category where is_leaf=1");
                }
                return categoryList;
            }
            categoryStr=categoryStr.substring(1,categoryStr.length()-1);
            if(type==1){
                categoryList = baseService.findMapBySQL("select id,name,'del' as action from qy_category where is_leaf=1 and id in ("+categoryStr+")");
            }else{
                categoryList = baseService.findMapBySQL("select id,name,'add' as action from qy_category where is_leaf=1 and id not in ("+categoryStr+")");
            }
            return categoryList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return categoryList;
    }
    /**
     * 排除分类表格ajax
     * @return
     */
    @RequestMapping("/discategory.do")
    @ResponseBody
    public Object discategory(Integer id,Integer type)  {
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        try {
            if(id==null){return mapList;}
            Coupon coupon = baseService.get(Coupon.class,id);
            String excludeCategory = coupon.getExcludeCategory();
            if(StringUtil.isEmptyOrNull(excludeCategory)||",".equals(excludeCategory)){
                if(type==2){
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_category where is_leaf=1");
                }
                return mapList;
            }
            excludeCategory=excludeCategory.substring(1,excludeCategory.length()-1);
            if(type==1){
                mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_category where is_leaf=1 and id in ("+excludeCategory+")");
            }else{
                mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_category where is_leaf=1 and id not in ("+excludeCategory+")");
            }
            return mapList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapList;
    }
    /**
     * 适用商家表格ajax
     * @return
     */
    @RequestMapping("/seller.do")
    @ResponseBody
    public Object seller(Integer page, Integer rows, Integer id,Integer type,String keyWord)  {
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        HibernatePage pager = new HibernatePage(page, rows);
        int total = 0;
        try {
            if(id==null){return mapList;}
            Coupon coupon = baseService.get(Coupon.class,id);
            String excludeSeller = coupon.getSuitPlatformSeller();
            if(StringUtil.isEmptyOrNull(excludeSeller)||",".equals(excludeSeller)){
                if(type==2){
                    if(!StringUtil.isEmptyOrNull(keyWord)){
                        mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_seller where status=1 and name like '%"+keyWord+"%'",pager);
                        total = baseService.getRecordCountSQL("select count(id) from qy_seller where status=1 and name like '%"+keyWord+"%'");
                    }else{
                        mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_seller where status=1",pager);
                        total = baseService.getRecordCountSQL("select count(id) from qy_seller where status=1");
                    }
                }
                return mapList;
            }
            excludeSeller=excludeSeller.substring(1,excludeSeller.length()-1);
            if(type==1){
                if(StringUtil.isEmptyOrNull(keyWord)){
                    mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_seller where id in ("+excludeSeller+")",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_seller where id in ("+excludeSeller+")");
                }else{
                    mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_seller where id in ("+excludeSeller+") and name like '%"+keyWord+"%'",pager );
                    total = baseService.getRecordCountSQL("select count(id) from qy_seller where id in ("+excludeSeller+") and name like '%"+keyWord+"%'");
                }
            }else{
                if(StringUtil.isEmptyOrNull(keyWord)){
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_seller where id not in ("+excludeSeller+") and status = 1",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_seller where status = 1 and id not in ("+excludeSeller+")");
                }else{
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_seller where status = 1 and id not in ("+excludeSeller+") and name like '%"+keyWord+"%'",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_seller where status = 1 and id not in ("+excludeSeller+") and name like '%"+keyWord+"%'");
                }
            }
            return new EasyUIResponse(total,mapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(total,mapList);
    }
    /**
     * 排除商家表格ajax
     * @return
     */
    @RequestMapping("/disseller.do")
    @ResponseBody
    public Object disseller(Integer page, Integer rows, Integer id,Integer type,String keyWord)  {
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        HibernatePage pager = new HibernatePage(page, rows);
        int total = 0;
        try {
            if(id==null){return mapList;}
            Coupon coupon = baseService.get(Coupon.class,id);
            String excludeSeller = coupon.getExcludeSeller();
            if(StringUtil.isEmptyOrNull(excludeSeller)||",".equals(excludeSeller)){
                if(type==2){
                    if(!StringUtil.isEmptyOrNull(keyWord)){
                        mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_seller where status=1 and name like '%"+keyWord+"%'",pager);
                        total = baseService.getRecordCountSQL("select count(id) from qy_seller where status=1 and name like '%"+keyWord+"%'");
                    }else{
                        mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_seller where status=1",pager);
                        total = baseService.getRecordCountSQL("select count(id) from qy_seller where status=1");
                    }
                }
                return mapList;
            }
            excludeSeller=excludeSeller.substring(1,excludeSeller.length()-1);
            if(type==1){
                if(StringUtil.isEmptyOrNull(keyWord)){
                    mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_seller where id in ("+excludeSeller+")",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_seller where id in ("+excludeSeller+")");
                }else{
                    mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_seller where id in ("+excludeSeller+") and name like '%"+keyWord+"%'",pager );
                    total = baseService.getRecordCountSQL("select count(id) from qy_seller where id in ("+excludeSeller+") and name like '%"+keyWord+"%'");
                }
            }else{
                if(StringUtil.isEmptyOrNull(keyWord)){
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_seller where id not in ("+excludeSeller+") and status = 1",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_seller where status = 1 and id not in ("+excludeSeller+")");
                }else{
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_seller where status = 1 and id not in ("+excludeSeller+") and name like '%"+keyWord+"%'",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_seller where status = 1 and id not in ("+excludeSeller+") and name like '%"+keyWord+"%'");
                }
            }
            return new EasyUIResponse(total,mapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(total,mapList);
    }
    /**
     * 适用产品表格ajax
     * @return
     */
    @RequestMapping("/product.do")
    @ResponseBody
    public Object product(Integer page, Integer rows, Integer id,Integer type,String keyWord)  {
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        HibernatePage pager = new HibernatePage(page, rows);
        int total = 0;
        try {
            if(id==null){return mapList;}
            Coupon coupon = baseService.get(Coupon.class,id);
            String suitPlatformProduct = coupon.getSuitPlatformProduct();
            if(StringUtil.isEmptyOrNull(suitPlatformProduct)||",".equals(suitPlatformProduct)){
                if(type==2){
                    if(!StringUtil.isEmptyOrNull(keyWord)){
                        mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_product where status=1 and name like '%"+keyWord+"%'",pager);
                        total = baseService.getRecordCountSQL("select count(id) from qy_product where status=1 and name like '%"+keyWord+"%'");
                    }else{
                        mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_product where status=1",pager);
                        total = baseService.getRecordCountSQL("select count(id) from qy_product where status=1");
                    }
                }
                return new EasyUIResponse(total,mapList);
            }
            suitPlatformProduct=suitPlatformProduct.substring(1,suitPlatformProduct.length()-1);
            if(type==1){
                if(StringUtil.isEmptyOrNull(keyWord)){
                    mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_product where id in ("+suitPlatformProduct+")",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_product where id in ("+suitPlatformProduct+")");
                }else{
                    mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_product where id in ("+suitPlatformProduct+") and name like '%"+keyWord+"%'",pager );
                    total = baseService.getRecordCountSQL("select count(id) from qy_product where id in ("+suitPlatformProduct+") and name like '%"+keyWord+"%'");
                }
            }else{
                if(StringUtil.isEmptyOrNull(keyWord)){
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_product where status = 1 and id not in ("+suitPlatformProduct+")",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_product where status = 1 and id not in ("+suitPlatformProduct+")");
                }else{
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_product where status = 1 and id not in ("+suitPlatformProduct+") and name like '%"+keyWord+"%'",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_product where status = 1 and id not in ("+suitPlatformProduct+") and name like '%"+keyWord+"%'");
                }
            }
            return new EasyUIResponse(total,mapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(0,mapList);
    }
    /**
     * 排除产品表格ajax
     * @return
     */
    @RequestMapping("/disproduct.do")
    @ResponseBody
    public Object disproduct(Integer page, Integer rows, Integer id,Integer type,String keyWord)  {
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        HibernatePage pager = new HibernatePage(page, rows);
        int total = 0;
        try {
            if(id==null){return mapList;}
            Coupon coupon = baseService.get(Coupon.class,id);
            String suitStr = coupon.getExcludeProduct();
            if(StringUtil.isEmptyOrNull(suitStr)||",".equals(suitStr)){
                if(type==2){
                    if(!StringUtil.isEmptyOrNull(keyWord)){
                        mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_product where status=1 and name like '%"+keyWord+"%'",pager);
                        total = baseService.getRecordCountSQL("select count(id) from qy_product where status=1 and name like '%"+keyWord+"%'");
                    }else{
                        mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_product where status=1",pager);
                        total = baseService.getRecordCountSQL("select count(id) from qy_product where status=1");
                    }
                }
                return new EasyUIResponse(total,mapList);
            }
            suitStr=suitStr.substring(1,suitStr.length()-1);
            if(type==1){
                if(StringUtil.isEmptyOrNull(keyWord)){
                    mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_product where id in ("+suitStr+")",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_product where id in ("+suitStr+")");
                }else{
                    mapList = baseService.findMapBySQL("select id,name,'del' as action from qy_product where id in ("+suitStr+") and name like '%"+keyWord+"%'",pager );
                    total = baseService.getRecordCountSQL("select count(id) from qy_product where id in ("+suitStr+") and name like '%"+keyWord+"%'");
                }
            }else{
                if(StringUtil.isEmptyOrNull(keyWord)){
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_product where status = 1 and id not in ("+suitStr+")",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_product where status = 1 and id not in ("+suitStr+")");
                }else{
                    mapList = baseService.findMapBySQL("select id,name,'add' as action from qy_product where status = 1 and id not in ("+suitStr+") and name like '%"+keyWord+"%'",pager);
                    total = baseService.getRecordCountSQL("select count(id) from qy_product where status = 1 and id not in ("+suitStr+") and name like '%"+keyWord+"%'");
                }
            }
            return new EasyUIResponse(total,mapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse(0,mapList);
    }
    /**
     * 编辑适用分类
     * @return
     */
    @RequestMapping("/editCategory.do")
    @ResponseBody
    public Object editCategory(Integer couponId,Integer editId,String type){
        try {
            Coupon coupon = baseService.get(Coupon.class,couponId);
            String suitPlatformCategory = coupon.getSuitPlatformCategory();
            if("del".equals(type)){//删除
                suitPlatformCategory=suitPlatformCategory.replace(editId+",","");
            }else{//添加
                if(StringUtil.isEmptyOrNull(suitPlatformCategory)){
                    suitPlatformCategory=","+editId+",";
                }else{
                    suitPlatformCategory+=editId+",";
                }
            }
            coupon.setSuitPlatformCategory(suitPlatformCategory);
            coupon.setUpdateTime(new Date());
            baseService.update(coupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /**
     * 编辑排除分类
     * @return
     */
    @RequestMapping("/editdisCategory.do")
    @ResponseBody
    public Object editdisCategory(Integer couponId,Integer editId,String type){
        try {
            Coupon coupon = baseService.get(Coupon.class,couponId);
            String excludeCategory = coupon.getExcludeCategory();
            if("del".equals(type)){//删除
                excludeCategory=excludeCategory.replace(editId+",","");
            }else{//添加
                if(StringUtil.isEmptyOrNull(excludeCategory)){
                    excludeCategory=","+editId+",";
                }else{
                    excludeCategory+=editId+",";
                }
            }
            coupon.setExcludeCategory(excludeCategory);
            coupon.setUpdateTime(new Date());
            baseService.update(coupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /**
     * 编辑适用商家
     * @return
     */
    @RequestMapping("/editSeller.do")
    @ResponseBody
    public Object editSeller(Integer couponId,Integer editId,String type){
        try {
            Coupon coupon = baseService.get(Coupon.class,couponId);
            String suitPlatformSeller = coupon.getSuitPlatformSeller();
            if("del".equals(type)){//删除
                suitPlatformSeller=suitPlatformSeller.replace(editId+",","");
            }else{//添加
                if(StringUtil.isEmptyOrNull(suitPlatformSeller)){
                    suitPlatformSeller=","+editId+",";
                }else{
                    suitPlatformSeller+=editId+",";
                }
            }
            coupon.setSuitPlatformSeller(suitPlatformSeller);
            coupon.setUpdateTime(new Date());
            baseService.update(coupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /**
     * 编辑排除商家
     * @return
     */
    @RequestMapping("/editdisSeller.do")
    @ResponseBody
    public Object editdisSeller(Integer couponId,Integer editId,String type){
        try {
            Coupon coupon = baseService.get(Coupon.class,couponId);
            String suitStr = coupon.getExcludeSeller();
            if("del".equals(type)){//删除
                suitStr=suitStr.replace(editId+",","");
            }else{//添加
                if(StringUtil.isEmptyOrNull(suitStr)){
                    suitStr=","+editId+",";
                }else{
                    suitStr+=editId+",";
                }
            }
            coupon.setExcludeSeller(suitStr);
            coupon.setUpdateTime(new Date());
            baseService.update(coupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /**
     * 编辑适用商品
     * @return
     */
    @RequestMapping("/editProduct.do")
    @ResponseBody
    public Object editProduct(Integer couponId,Integer editId,String type){
        try {
            Coupon coupon = baseService.get(Coupon.class,couponId);
            String suitStr = coupon.getSuitPlatformProduct();
            if("del".equals(type)){//删除
                suitStr=suitStr.replace(editId+",","");
            }else{//添加
                if(StringUtil.isEmptyOrNull(suitStr)){
                    suitStr=","+editId+",";
                }else{
                    suitStr+=editId+",";
                }
            }
            coupon.setSuitPlatformProduct(suitStr);
            coupon.setUpdateTime(new Date());
            baseService.update(coupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /**
     * 编辑排除商品
     * @return
     */
    @RequestMapping("/editdisProduct.do")
    @ResponseBody
    public Object editdisProduct(Integer couponId,Integer editId,String type){
        try {
            Coupon coupon = baseService.get(Coupon.class,couponId);
            String suitStr = coupon.getExcludeProduct();
            if("del".equals(type)){//删除
                suitStr=suitStr.replace(editId+",","");
            }else{//添加
                if(StringUtil.isEmptyOrNull(suitStr)){
                    suitStr=","+editId+",";
                }else{
                    suitStr+=editId+",";
                }
            }
            coupon.setExcludeProduct(suitStr);
            coupon.setUpdateTime(new Date());
            baseService.update(coupon);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }

}